package com.magic.gre.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.magic.gre.R;
import com.magic.gre.entity.ThesaurusBean;
import com.magic.gre.helper.BannerAdapterHelper;
import com.noname.lib_base_java.adapter.CommonAdapter;
import com.noname.lib_base_java.adapter.ViewHolder;
import com.noname.lib_base_java.glide.LoadImageUtils;
import com.noname.lib_base_java.util.PathUtil;

import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2019-01-18 15:35
 * 文件名: ThesaurusAdapter2
 * 描述:
 */
public class ThesaurusAdapter2 extends CommonAdapter<ThesaurusBean> {


    private OnThesaurusAdapter2Listener onThesaurusAdapter2Listener;

    public void setOnThesaurusAdapter2Listener(OnThesaurusAdapter2Listener onThesaurusAdapter2Listener) {
        this.onThesaurusAdapter2Listener = onThesaurusAdapter2Listener;
    }

    private RequestOptions requestOptions = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
    private BannerAdapterHelper mBannerAdapterHelper = new BannerAdapterHelper();

    public ThesaurusAdapter2(Context mContext, List<ThesaurusBean> mData) {
        super(mContext, mData);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.item_thesaurus, parent, false);
        mBannerAdapterHelper.onCreateViewHolder(parent, itemView);
        return new ViewHolder(itemView);
    }

    @Override
    protected void bindData(ViewHolder holder, final int position) {
        if (mData!=null&&!mData.isEmpty()) {
            final ThesaurusBean t = mData.get(position % mData.size());
            mBannerAdapterHelper.onBindViewHolder(holder.itemView, position, getItemCount());
            ImageView imageView = holder.getView(R.id.mImageView);
            LoadImageUtils.loadImage(PathUtil.imgPath(t.getBannerData().getPath()), imageView, requestOptions);

            TextView tv1 = holder.getView(R.id.tv1);
            TextView tv2 = holder.getView(R.id.tv2);

            String color = t.getColor();
            if (color != null && TextUtils.isEmpty(color)) {
                tv1.setTextColor(Color.parseColor(t.getColor()));
                tv2.setTextColor(Color.parseColor(t.getColor()));
            }

            String totalStr = String.valueOf(t.getTotal());
            String strHint = " /";
            String numStr = String.valueOf(t.getNum());
            String content1 = numStr + strHint + totalStr + " 词";

            int start1 = content1.indexOf(numStr);
            int end1 = start1 + numStr.length();

            int start2 = content1.indexOf(strHint);
            int end2 = start2 + strHint.length();

            SpannableString styledText1 = new SpannableString(content1);
            styledText1.setSpan(new TextAppearanceSpan(mContext, R.style.ThesaurusBigStyle1), start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText1.setSpan(new TextAppearanceSpan(mContext, R.style.ThesaurusBigStyle2), start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            tv1.setText(styledText1, TextView.BufferType.SPANNABLE);

            String countStr = String.valueOf(t.getNo());
            String toDayNum = String.valueOf(t.getToday());
            String hintStr = " 今日背词数量 ";
            String content2 = "遍数" + countStr + " 今日背词数量 " + toDayNum;

            int start3 = content2.indexOf(countStr);
            int end3 = start3 + countStr.length();

            int start4 = content2.indexOf(toDayNum, content2.indexOf(hintStr));
            int end4 = start4 + toDayNum.length();

            SpannableString styledText2 = new SpannableString(content2);
            styledText2.setSpan(new TextAppearanceSpan(mContext, R.style.ThesaurusBigStyle3), start3, end3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            styledText2.setSpan(new TextAppearanceSpan(mContext, R.style.ThesaurusBigStyle3), start4, end4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            tv2.setText(styledText2, TextView.BufferType.SPANNABLE);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onThesaurusAdapter2Listener != null) {
                        onThesaurusAdapter2Listener.onThesaurusItemClick(t.getId(), position);
                    }

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return Integer.MAX_VALUE;
    }

    public interface OnThesaurusAdapter2Listener{
        void onThesaurusItemClick(String id,int position);
    }
}
